﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
using System.Windows.Controls;

namespace Group_Project_Book_Store.DatabaseClasses
{
    /// <summary>
    /// Class used to access the database.
    /// </summary>
    class DatabaseAccess
    {
        /// <summary>
        /// Connection string to the database.
        /// </summary>
        private string ConnectionString;
        /// <summary>
        /// Constructor that sets the connection string to the database
        /// </summary>
        public DatabaseAccess()
        {
            ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source= " + Directory.GetCurrentDirectory() + "\\Database\\Invoices.mdb";
        }
        public DataSet ExecuteSelectQuery(string SQLStatement)
        {
            try
            {
                DataSet ds = new DataSet();

                using (OleDbConnection conn = new OleDbConnection(ConnectionString))
                {
                    using (OleDbDataAdapter adapater = new OleDbDataAdapter())
                    {
                        conn.Open();

                        adapater.SelectCommand = new OleDbCommand(SQLStatement, conn);
                        adapater.SelectCommand.CommandTimeout = 0;

                        adapater.Fill(ds);
                    }
                }
                
                return ds;
            }
            catch (Exception ex)
            {

                throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
            }
        }
        public int ExecuteNonQuery(string SQLStatement)
        {
            try
            {
                int numberOfRowsUpdated;

                using (OleDbConnection conn = new OleDbConnection(ConnectionString))
                {
                    conn.Open();

                    OleDbCommand cmd = new OleDbCommand(SQLStatement, conn);
                    cmd.CommandTimeout = 0;

                    numberOfRowsUpdated = cmd.ExecuteNonQuery();
                }
                return numberOfRowsUpdated;
            }
            catch (Exception ex)
            {

                throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
            }
        }
    }
}
